/**
 * 
 */
package com.google.code.cisx.servlets;

import java.util.Enumeration;
import java.util.NoSuchElementException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @version $Id: RemoteServiceBasedActionBeanSupport.java 258 2008-11-29 06:23:51Z rahul.thakur.xdev $
 * @since 1.0
 */
public abstract class RemoteServiceBasedActionBeanSupport extends RemoteServiceServlet implements ActionBean {

  private static final long serialVersionUID = 2841984222641762560L;

  protected ActionBeanContext context;

  @DefaultHandler
  public Resolution defaultHandler() {

    return new Resolution() {

      public void execute(HttpServletRequest request1, HttpServletResponse response1) throws Exception {
        doPost(request1, response1);
      }
    };
  }

  public ActionBeanContext getContext() {
    return context;
  }

  public void setContext(final ActionBeanContext context) {
    this.context = context;
    // GWT servlet needs to be initialized with the servletConfig -
    // otherwise it can't access the servletContext
    try {
      super.init(new ServletConfig() {
        public String getInitParameter(String name) {
          return null;
        }

        public Enumeration<?> getInitParameterNames() {
          return new Enumeration<String>() {

            public boolean hasMoreElements() {
              return false;
            }

            public String nextElement() {
              throw new NoSuchElementException();
            }
          };
        }

        public ServletContext getServletContext() {
          return context.getServletContext();
        }

        public String getServletName() {
          // return getClass().getName();
          return getActionBeanClass().getName();
        }
      });
    } catch (ServletException e) {
      throw new RuntimeException(e);
    }
  }

  public Class<?> getActionBeanClass() {
    return getClass();
  }

}
